from lxml import etree 
import time
root = etree.Element("root")
# print(root.tag)
root.append(etree.Element("child1"))
child2 = etree.SubElement(root, "child2")
child3 = etree.SubElement(root, "child3")
# print(etree.tostring(root, pretty_print=True))
child = root[0]
# print(child.tag)
# print(len(root))
# print(root.index(root[1]))
children = list(root)
# print(children)
# for child in root:
#     print(child.tag)
root.insert(0,etree.Element("child0"))
start = root[:1]
end = root[-1:]
# print(len(start),end)
# print(start[0].tag)
# print(end[0].tag)
root[0]=root[-1]
# for child in root:
#     print(child.tag)

from copy import deepcopy
# element= etree.Element("neu")
# element.append(deepcopy(root[1]))
# print(element[0].tag)
# print([c.tag for c in root]) #['child3', 'child1', 'child2']
# print(root[1].getprevious())
# print(root[1].getnext()) #<Element child2 at 0x1b305da4fc0>
# root = etree.Element("root", interesting="totally")
# print(etree.tostring(root)) #b'<root interesting="totally"/>'
# root.set("hello","huhu")
# print(etree.tostring(root)) #b'<root interesting="totally" hello="huhu"/>'
# print(sorted(root.keys())) #['hello', 'interesting']
for name, value in sorted(root.items()):
    print('%s = %r' % (name, value))  # %r将变量传到repr()函数

# attributes = root.attrib
# print(type(attributes))
# print(attributes["interesting"])
# root.text="TEXT"
# print(etree.tostring(root))

# html=etree.Element("html")
# body = etree.SubElement(html,"body")
# body.text="TEXT"
# print(etree.tostring(html))
# br=etree.SubElement(body,"br")
# print(etree.tostring(html))
# br.tail = "TAIL"
# print(etree.tostring(html))

start=time.time()
from lxml.html import parse, submit_form
import requests,logging
# url='http://quotes.money.163.com/1002108.html'
url="https://www.xuetangx.com/learn/bnuP0701042103/bnuP0701004733/7545382/video/11363883"
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"}
response = requests.get(url, headers= headers)
html=etree.HTML(response.text)
logging.basicConfig(filename='./response_text.log',filemode='w',level=logging.DEBUG)
logging.info(response.text.encode('gbk').decode('gbk'))
# stockinfo=html.xpath('//span[@class="title"]/following-sibling::*')[2].text
stockinfo=html.xpath('//video')
print(stockinfo)
print("执行时间 {} 秒".format(round(time.time() - start, 2)))

